package YZ.Students.Mier.controller.system; /**
 * @className: ${NAME}
 * @author: shuishui
 * @date: 2022/12/03 17:16
 */

import YZ.Students.Mier.Service.SystemUser;
import YZ.Students.Mier.Service.impl.SystemUserServiceImpl;
import YZ.Students.Mier.Tool.utils.BaseConstants;
import YZ.Students.Mier.controller.BaseServlet;
import YZ.Students.Mier.entity.SystemUserEntity;
import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SystemUserServlet/*")
public class SystemUserServlet extends BaseServlet {
    private SystemUser systemUserService = new SystemUserServiceImpl();
    public void list() throws Exception {
        // 1.查询数据
        List<SystemUserEntity> list = systemUserService.list();
        // 2.把数据放到Map中
        Map<String, Object> map = new HashMap<>();
        map.put("sysUserList", list);
        System.out.println(map);
        // 3.跳转到视图
        outView("system/sysUser/sysUserList", map);
    }


    public String add() throws Exception {
        String json = super.req.getReader().readLine();
        SystemUserEntity systemUser = JSON.parseObject(json, SystemUserEntity.class);
        boolean save = systemUserService.save(systemUser);
        return outJson(save);
    }

    public void systemUserById() throws Exception {
        String id = this.req.getParameter("id");
        SystemUserEntity systemUser = systemUserService.byId(Integer.parseInt(id));
        Map<String, Object> map = new HashMap<>();
        map.put("systemUser", systemUser);
        outView("/system/sysUser/sysUserUpdate", map);
    }

    public String update() throws Exception {
        String json = super.req.getReader().readLine();
        SystemUserEntity systemUser = JSON.parseObject(json, SystemUserEntity.class);
        boolean save = systemUserService.update(systemUser);
        return outJson(save);
    }

    public String delete() throws Exception {
        String id = super.req.getParameter("id");
        boolean remove = systemUserService.remove(Integer.parseInt(id));
        return BaseConstants.BASE_REDIRECT + ":" + "SystemUserServlet/list";
    }

    public String batchDel() throws Exception {
        String json = req.getReader().readLine();
        List<Integer> list = JSON.parseArray(json, Integer.class);
        return outJson(systemUserService.batchRemove(list));
    }
}
